package com.rjgc.title.controller.apply;

import com.rjgc.title.common.result.Result;
import com.rjgc.title.model.entity.apply.ResearchProject;
import com.rjgc.title.model.entity.apply.ResearchResultsExpert;
import com.rjgc.title.service.ResearchProjectService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: title-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-26 12:00
 **/
@Api(tags = "科研项目(课题)")
@RestController
@RequestMapping("/api/researchp")
public class ResearchProjectController {

    @Autowired
    private ResearchProjectService researchProjectService;

    /**
     * 保存用户
     * @param request
     * @param researchProject
     * @return
     */
    @PostMapping("inner/save")
    public Result save(HttpServletRequest request, @RequestBody ResearchProject researchProject) {
        // 获取当前对象的id
        String userId = (String) request.getSession().getAttribute("userId");
        researchProject.setUserId(Long.parseLong(userId));
        // 调用service方法进行保存
        researchProjectService.save(researchProject);
        return Result.ok();
    }

    /**
     * 根据用户id查询所有
     *
     * @param request
     * @return
     */
    @GetMapping("inner/getAll")
    public Result getAll(HttpServletRequest request) {
        // 获取当前对象的id
        String userId = (String) request.getSession().getAttribute("userId");
        List<ResearchProject> researchProjectList  = researchProjectService.getAll(userId);

        return Result.ok(researchProjectList);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("inner/getById/{id}")
    public Result getById(@PathVariable("id") Long id) {
        // 调用service方法进行查询
        ResearchProject researchProject = researchProjectService.getById(id);

        // 返回数据
        return Result.ok(researchProject);
    }

    /**
     *  根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("inner/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        // 调用service方法进行查询
        researchProjectService.removeById(id);

        return Result.ok();
    }

    /**
     * 更新操作
     * @param researchProject
     * @return
     */
    @PostMapping("inner/updateById")
    public Result updateById(@RequestBody ResearchProject researchProject) {
        // 调用service方法进行更新
        researchProjectService.updateById(researchProject);

        return Result.ok();
    }
}
